/**
 * 
 */
package com.honghao.student.excel;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.honghao.student.pojo.Student;

/**
 * @author honghao.li
 * 
 */
public class StudentListExcelView extends AbstractExcelView {

	@Override
	@SuppressWarnings("unchecked")
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		response.setHeader("Content-Disposition", "attachment;filename="
				+ new String("学生列表".getBytes(), "iso8859-1"));
		List<Student> stuList = (List<Student>) model.get("stuList");
		HSSFSheet sheet = workbook.createSheet("stuList");
		HSSFRow header = sheet.createRow(0);
		header.createCell(0).setCellValue("ID");
		header.createCell(1).setCellValue("姓名");
		header.createCell(2).setCellValue("密码");
		header.createCell(3).setCellValue("邮箱");
		header.createCell(4).setCellValue("创建时间");

		int rowNum = 1;
		for (Student stu : stuList) {
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(stu.getStuId());
			row.createCell(1).setCellValue(stu.getStuName());
			row.createCell(2).setCellValue(stu.getPassword());
			row.createCell(3).setCellValue(stu.getEmail());
			row.createCell(4).setCellValue(
					DateFormatUtils.format(stu.getCtime(), "yyyy-MM-dd"));
		}
	}

}
